package com.hongkuncheng.cms.controller.admin;

import java.io.File;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.FileHelper;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.model.Zhuanti;
import com.hongkuncheng.cms.model.Columns;
import com.hongkuncheng.cms.model.ColumnsType;
import com.hongkuncheng.cms.model.Datamodel;
import com.hongkuncheng.cms.model.Webinfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 专题频道栏目管理
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/zhuanticolumns")
public class AzhuanticolumnsController extends BaseController {

    // 专题栏目列表
    @RequestMapping
    public String index() {
        int zhuantiId = Integer.parseInt(request.getParameter("zhuantiid"));
        Zhuanti zhuanti = factorySession.get(Zhuanti.class, zhuantiId);
        request.setAttribute("zhuanti", zhuanti);
        List<Columns> columnses = factorySession.createCriteria(Columns.class)
                .add(Restrictions.eq("columnsType.id", 6))
                .add(Restrictions.eq("zhuanti.id", zhuantiId))
                .addOrder(Order.asc("sort")).list();
        request.setAttribute("columnses", columnses);
        return view();
    }

    // 专题栏目编辑
    @RequestMapping("edit")
    public String edit() {
        int zhuantiId = Integer.parseInt(request.getParameter("zhuantiid"));
        int columnsId = request.getParameter("columnsid") == null ? 0 :
                Integer.parseInt(request.getParameter("columnsid"));
        Columns columns = columnsId == 0 ? new Columns() : factorySession.get(Columns.class, columnsId);
        if (columnsId == 0) {
            columns.setColumnsType(factorySession.load(ColumnsType.class, 6));
            columns.setAutosingle(true);
            Object uniqueResult = factorySession.createCriteria(Columns.class)
                    .add(Restrictions.eq("columnsType.id", 6))
                    .add(Restrictions.eq("zhuanti.id", zhuantiId))
                    .setProjection(Projections.max("sort"))
                    .uniqueResult();
            int maxSort = uniqueResult == null ? 0 : Integer.parseInt(uniqueResult.toString());
            columns.setSort(++maxSort);
            Zhuanti zhuanti = factorySession.load(Zhuanti.class, zhuantiId);
            columns.setZhuanti(zhuanti);
        }
        request.setAttribute("columns", columns);
        request.setAttribute("zhuantiid", zhuantiId);
        List<Datamodel> datamodels = factorySession.createCriteria(Datamodel.class)
                .add(Restrictions.eq("datamodelType.id", 3)).list();
        request.setAttribute("datamodels", datamodels);
        webinfo = factorySession.get(Webinfo.class, 1);
        List<File> templatefiles = FileHelper.getFiles(applicationDirectory + "/template/" +
                webinfo.getTemplate() + "/");
        request.setAttribute("templatefiles", templatefiles);
        return view();
    }

    // 专题栏目编辑处理
    @RequestMapping("editdo")
    @ResponseBody
    public void editdo() {
        checkAdminPermission("yunwei_webmaintain_zhuanti");
        int zhuantiId = Integer.parseInt(request.getParameter("zhuantiid"));
        int columnsId = Integer.parseInt(request.getParameter("columnsid"));
        String name = request.getParameter("name");
        int exist = Integer.parseInt(factorySession.createCriteria(Columns.class)
                .add(Restrictions.eq("columnsType.id", 6))
                .add(Restrictions.eq("name", name))
                .add(Restrictions.ne("id", columnsId))
                .add(Restrictions.eq("zhuanti.id", zhuantiId))
                .setProjection(Projections.count("id"))
                .uniqueResult().toString());
        if (exist > 0) {
            PageHelper.alertAndGoBack("栏目名称已存在，请重新输入");
            return;
        }
        String urldirectory = request.getParameter("urldirectory");
        Columns columns = columnsId == 0 ? new Columns() :
                factorySession.load(Columns.class, columnsId, LockMode.PESSIMISTIC_WRITE);
        columns.setColumnsType(factorySession.load(ColumnsType.class, 6));
        columns.setName(name);
        columns.setUrldirectory(urldirectory);
        Integer datamodelId = Integer.parseInt(request.getParameter("datamodelid"));
        columns.setDatamodel(factorySession.load(Datamodel.class, datamodelId));
        columns.setTemplatelist(request.getParameter("templatelist"));
        columns.setTemplatedetail(request.getParameter("templatedetail"));
        columns.setAutosingle(request.getParameter("autosingle") != null);
        Zhuanti zhuanti = factorySession.load(Zhuanti.class, zhuantiId);
        columns.setZhuanti(zhuanti);
        columns.setTarget(request.getParameter("target"));
        columns.setSort(Integer.parseInt(request.getParameter("sort")));
        columns.setParentId(0);
        if (columnsId == 0) factorySession.save(columns);
        recordLog((columnsId == 0 ? "新建" : "更新") + "专题栏目《" + columns.getName().trim() + "》");
        PageHelper.alertAndRedirect("保存成功", ".?zhuantiid=" + zhuantiId);
    }

    // 专题栏目编辑删除
    @RequestMapping("delete")
    @ResponseBody
    public void delete() {
        checkAdminPermission("yunwei_webmaintain_zhuanti");
        int zhuantiId = Integer.parseInt(request.getParameter("zhuantiid"));
        int columnsId = Integer.parseInt(request.getParameter("columnsid"));
        Columns columns = factorySession.load(Columns.class, columnsId, LockMode.PESSIMISTIC_WRITE);
        factorySession.delete(columns);
        recordLog("删除专题栏目《" + columns.getName().trim() + "》");
        PageHelper.alertAndRedirect("删除成功", "./?zhuantiid=" + zhuantiId);
    }

}
